package com.ruan.service;

import com.ruan.dao.OrdersMapper;
import com.ruan.entity.Orders;
import com.ruan.entity.Result;
import com.ruan.openfeign.GoodsCenterOpenfeign;
import com.ruan.query.OrdersQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName:OrdersService
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/7/3 22:45
 **/
@Service
public class OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private GoodsCenterOpenfeign goodsCenterOpenfeign;
    public Result addOrders(Orders orders) {
        if(orders.getOrderAddress()==null||orders.getOrderAddress()==""){
            return new Result(100,"地址不能为空");
        }
        Date now = new Date();
        String currentTime = String.format("%tF %<tT", now);
        orders.setOrderReleaseTime(currentTime);
        int row = ordersMapper.addOrders(orders);
        int ordersId = orders.getId();
        if(row != 1){
            return new Result(100,"下单失败");
        }else {
            return new Result(200,String.valueOf(ordersId));
        }
    }

    public List<Orders> getOrdersList(OrdersQuery ordersQuery) {
        return ordersMapper.getOrdersList(ordersQuery);
    }

    public Result deleteOrders(Integer id) {
        int row = ordersMapper.deleteOrders(id);
        if(row != 1){
            return new Result(100,"订单删除失败");
        }else {
            return new Result(200,"订单删除成功");
        }
    }

    public Result deleteOrdersByIds(Integer[] ids) {
        int row = ordersMapper.deleteOrdersByIds(ids);
        if(row != ids.length){
            return new Result(100,"订单删除失败");
        }else {
            return new Result(200,"订单删除成功");
        }
    }

    public List<Orders> getAllOrdersList(OrdersQuery ordersQuery) {
        return ordersMapper.getAllOrdersList(ordersQuery);
    }

    public Result paySuccess(Integer id) {
        int row = ordersMapper.paySuccess(id);
        Orders orders = ordersMapper.getOrdersById(id);
        goodsCenterOpenfeign.addSales(orders.getGoodsId(),orders.getOrderCount());
        if(row != 1){
            return new Result(100);
        }else {
            return new Result(200);
        }
    }
}
